﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DatabaseExtension.ConnectionString
{
    /// <summary>
    /// Provide simple Interface for storing connection Info.
    /// </summary>
    public abstract class ConnectionInfo
    {
        /// <summary>
        /// Database Provider Factory. 
        /// </summary>
        public System.Data.Common.DbProviderFactory ClientFactory;
        /// <summary>
        /// Contains the base type of the connection string builder.
        /// </summary>
        protected virtual System.Data.Common.DbConnectionStringBuilder ConnectionStringBuilder
        {
            get;
            set;
        }
        /// <summary>
        /// Selected Database Type.
        /// </summary>
        public virtual Databases DatabaseType { get; protected set; }
        /// <summary>
        /// Database Name (Instance Name or PC Name) or path to the database file.
        /// </summary>
        public virtual String Server { get; protected set; }
        /// <summary>
        /// Working Database User
        /// </summary>
        public virtual String UserName { get; protected set; }
        /// <summary>
        /// Database (User) Password
        /// </summary>
        public virtual String Password { get; protected set; }
        /// <summary>
        /// Get the connection String.
        /// </summary>
        public virtual String ConnectionString
        {
            get
            {
                return this.ConnectionStringBuilder.ConnectionString;
            }
        }
        /// <summary>
        /// Create default SQL connection with username password
        /// </summary>
        /// <param name="type">Database Engine to use.</param>
        /// <param name="server">Instance name of the database or path to the database file.</param>
        /// <param name="user">Database user</param>
        /// <param name="password">Authentication password.</param>
        public ConnectionInfo(Databases type, String server, String user, String password)
        {
            this.DatabaseType = type;
            this.Server = server;
            this.UserName = user;
            this.Password = password;
            this.ConnectionStringBuilder = this.GetConnectionStringbuilder();
        }
        /// <summary>
        /// Creat new DbConnectionStringBuilder based on Connection String Builder.
        /// </summary>
        /// <param name="type">Database Type.</param>
        /// <param name="cecsb">Base Connection String Builder</param>
        public ConnectionInfo(Databases type, System.Data.Common.DbConnectionStringBuilder csb)
        {
            this.ConnectionStringBuilder = csb;
            this.DatabaseType = type;
        }
        /// <summary>
        /// Check for currect implementation of the Database Parameter.
        /// </summary>
        /// <param name="throwException">Throw Argument Exception if value is true.</param>
        /// <param name="typeFullName">Full Name of the current using Type.</param>
        public void CheckParameterOnDatabaseType(System.Data.Common.DbParameter param)
        {
            Type type = param.GetType(),
                 currentType = this.ClientFactory.CreateParameter().GetType();
            if (Type.Equals(type, currentType) == false)
                throw new ArgumentException(String.Format(DatabaseExtension.Properties.Resources.WrongDatabaseParameter, this.DatabaseType, type.FullName), "param");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected virtual System.Data.Common.DbConnectionStringBuilder GetConnectionStringbuilder()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// Create new Db Parameter
        /// </summary>
        /// <param name="name">Parameter Name</param>
        /// <param name="value">Parameter Value</param>
        /// <param name="type">DbType depend on Database</param>
        /// <param name="isNullable">if true, empty object will be converted to DbNull.Value</param>
        /// <param name="Size">Object Size.</param>
        /// <returns>Return new DbParameter</returns>
        public virtual System.Data.Common.DbParameter CreateParameter(String name, Object value, Object type, Boolean isNullable, int Size = 0)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// Create new Db Parameter. Autodetect parameter
        /// </summary>
        /// <param name="name">Parameter Name</param>
        /// <param name="value">Parameter Value</param>
        /// <param name="isNullable">if true, empty object will be converted to DbNull.Value</param>
        /// <param name="Size">Object Size.</param>
        /// <returns>Return new DbParameter</returns>
        public virtual System.Data.Common.DbParameter CreateParameter(String name, Object value, Boolean isNullable, int Size = 0)
        {
            throw new NotImplementedException();
        }
    }
}
